package cn.com.sbell.vss.commander;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.license.VssLicense;
import cn.com.sbell.vss.util.XmlUtil;

public class ShowLicense implements ICommander {
	private static final Log log = LogFactory.getLog(ShowLicense.class);

	public Response execute(HttpServletRequest request) {
		if(log.isDebugEnabled()){
		log.debug("Show License ...");
		}
		Response response = Response.createResponse(request);
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("parameters");

		VssLicense lic = (VssLicense) request
				.getAttribute(VssConstants.KEY_VSS_LICENSE);
		String hostId = (String) request
				.getAttribute(VssConstants.KEY_VSS_HOST_ID);
		if (lic == null) {
			throw new VssException(VssConstants.CODE_INVALID_LICENSE,
					new String[] { "License NOT found!", hostId });
		}
		XmlUtil.addElement(root, "hostId", lic.getHostId());
		XmlUtil.addElement(root, "validDate", Util.getDefaultDateFormat().format(
				lic.getValidDate()));
		XmlUtil.addElement(root, "expiringDate", Util.getDefaultDateFormat()
				.format(lic.getExpiringDate()));
		XmlUtil.addElement(root, "maxCameraAmount", lic.getMaxCameraAmount());
		XmlUtil.addElement(root, "maxUserAmount", lic.getMaxUserAmount());

		response.setParameters(doc.asXML());

		return response;
	}
}
